import { axiosInstance } from '../interceptors/index.js'

// 获取服务器状态
export const getServerStatus = async () => {
  try {
    const response = await axiosInstance.get('/api/auth/server-status')
    return response.data
  } catch (error) {
    console.error('获取服务器状态失败:', error)
    throw error
  }
}

// 用户登录
export const login = async (loginData) => {
  try {
    const response = await axiosInstance.post('/api/auth/login', loginData)
    return response.data
  } catch (error) {
    console.error('登录失败:', error)
    throw error
  }
}

// 用户注册
export const register = async (registerData) => {
  try {
    const response = await axiosInstance.post('/api/auth/register', registerData)
    return response.data
  } catch (error) {
    console.error('注册失败:', error)
    throw error
  }
}

// 获取用户信息
export const getUserInfo = async () => {
  try {
    console.log('[serverApi] 开始调用getUserInfo')
    console.log('[serverApi] 当前localStorage中的access-key:', localStorage.getItem('access-key'))
    console.log('[serverApi] axiosInstance baseURL:', axiosInstance.defaults.baseURL)
    console.log('[serverApi] 完整请求URL:', axiosInstance.defaults.baseURL + '/api/auth/userinfo')
    
    const response = await axiosInstance.get('/api/auth/userinfo')
    console.log('[serverApi] getUserInfo请求成功，响应:', response)
    console.log('[serverApi] 响应数据:', response.data)
    return response.data
  } catch (error) {
    console.error('[serverApi] getUserInfo请求失败:', error)
    console.error('[serverApi] 错误详情:', {
      message: error.message,
      response: error.response,
      request: error.request,
      config: error.config,
      code: error.code
    })
    throw error
  }
}

// 用户登出
export const logout = async () => {
  try {
    const response = await axiosInstance.post('/api/auth/logout')
    return response.data
  } catch (error) {
    console.error('登出失败:', error)
    throw error
  }
}

// Token刷新
export const refreshToken = async (refreshTokenData) => {
  try {
    const response = await axiosInstance.post('/api/auth/refresh', refreshTokenData)
    return response.data
  } catch (error) {
    console.error('Token刷新失败:', error)
    throw error
  }
}

// 模拟数据API
export const getNewsData = () => {
  return [
    {
      id: 1,
      title: '服务器正式开放！',
      date: '2024-01-15',
      content: '欢迎来到KBB服务器！我们是一个充满活力的Minecraft社区...',
      imageUrl: '/images/news1.jpg'
    },
    {
      id: 2,
      title: '新功能上线公告',
      date: '2024-01-10',
      content: '我们新增了多个实用功能，包括皮肤系统、传送系统等...',
      imageUrl: '/images/news2.jpg'
    }
  ]
}

export const getStaffData = () => {
  return [
    {
      id: 1,
      name: 'Admin',
      role: '服务器管理员',
      avatarUrl: '/images/avatar1.jpg',
      bio: '负责服务器的日常维护和管理工作'
    },
    {
      id: 2,
      name: 'Moderator',
      role: '游戏管理员',
      avatarUrl: '/images/avatar2.jpg',
      bio: '负责游戏内的秩序维护和玩家支持'
    }
  ]
}

export const getGalleryData = () => {
  return [
    {
      id: 1,
      title: '主城风光',
      url: '/images/gallery1.jpg'
    },
    {
      id: 2,
      title: '玩家建筑',
      url: '/images/gallery2.jpg'
    }
  ]
}

export const getDonorData = () => {
  return [
    {
      id: 1,
      name: '热心玩家A',
      amount: 100,
      date: '2024-01-15',
      message: '支持服务器发展！'
    },
    {
      id: 2,
      name: '热心玩家B',
      amount: 50,
      date: '2024-01-10',
      message: '希望服务器越来越好'
    }
  ]
}

export const getBanListData = () => {
  return [
    {
      playerName: '违规玩家1',
      uuid: 'uuid1',
      reason: '恶意破坏',
      banDate: '2024-01-15',
      unbanDate: '2024-02-15'
    },
    {
      playerName: '违规玩家2',
      uuid: 'uuid2',
      reason: '使用外挂',
      banDate: '2024-01-10',
      unbanDate: '永久封禁'
    }
  ]
}